#include <iostream>
#include <string>
using namespace std;


template <class K, class V>
struct BSTreeNode {
	//  pair<K, V> //键值对
	K _key;
	V _value;
	BSTreeNode<K, V>* _left;
	BSTreeNode<K, V>* _right;

	BSTreeNode(const K& key, const V& value)
		:_key(key)
		, _value(value)
		, _left(nullptr)
		, _right(nullptr)
	{}
};

template<class K, class V>
class BSTree
{
	typedef BSTreeNode<K, V> Node;
public:
	BSTree() = default;
	BSTree(const BSTree<K, V>& t)
	{
		_root = copy(t._root);
	}

	Node* copy(Node* root)
	{
		if (root == nullptr) return nullptr;
		Node* newroot = new Node(root->_left, root->_right);
		newroot->_left = copy(root->_left);
		newroot->_right = copy(root->_right);
		return newroot;
 	}

	~BSTree()
	{
		Destroy(_root);
		_root = nullptr;
	}
	void Destroy(Node *root)
	{
		if (root == nullptr) return;
		Destroy(root->_left);
		Destroy(root->_right);
		delete root;
	}


	bool Insert(const K& key, const V& value)
	{
		if (_root == nullptr) {
			_root = new Node(key, value);
			return true;
		}

		Node* cur = _root, * parent = nullptr; //父母保存cur的前置
		while (cur)
		{
			if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else return false;
		}
		cur = new Node(key, value);
		if (parent->_key < key) parent->_right = cur;
		else parent->_left = cur;

		return true;
		
	}
	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key < key) cur = cur->_right;
			else if (cur->_key > key) cur = cur->_left;
			else return cur;
		}
		return nullptr;
	}
	bool Erase(const K& key) 
	{
		Node* parent = nullptr, * cur = _root;
		while (cur)
		{
			if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_right > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				if (cur->_left == nullptr) {
					if (parent == nullptr) //当前节点为根节点
						_root = cur->_right;
					else
					{
						if (parent->_left == cur)
							parent->_left = cur->_right;
						else  parent->_right = cur->_right;
					}
					delete cur;
					return true;
				}
				else if (cur->_right == nullptr)
				{
					if (parent == nullptr)
						_root = cur->_left;
					else
					{
						if (parent->_left == cur)
							parent->_left = cur->_left;
						else parent->_right = cur->_left;
					}
					delete cur;
					return true;
				}
				else
				{
					Node* rightMinp = cur, rightMin = cur->_right; 
					//找右子树的最小节点进行交换
					while (rightMin->_left)
					{
						rightMinp = rightMin;
						rightMin = rightMin->_left;
					}
					cur->_key = rightMin->_key;
					if (rightMinp->_left == rightMin) rightMinp->_left = rightMin->_right;
					else rightMinp->_right = rightMin->_right;

					return true;
				}
			}
		}
		return false;
	}
	
	void _InOrder(Node* root)
	{
		if (root == nullptr) return ;
		_InOrder(root->_left);
		cout << root->_key << ":" << root->_value << endl;
		_InOrder(root->_right);
	}

	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}
	
private:
	Node* _root = nullptr;
};

void TestBSTree()
{
	BSTree<string, string> dict;
	dict.Insert("insert", "插入");
	dict.Insert("erase", "删除");
	dict.Insert("left", "左边");
	dict.Insert("string", "字符串");

	string str;
	//dict.InOrder();
	while (cin >> str)
	{
		auto ret = dict.Find(str);
		if (ret)
		{
			cout << str << ":" << ret->_value << endl;
		}
		else
		{
			cout << "单词拼写错误" << endl;
		}
	}

	string strs[] = { "苹果", "西瓜", "苹果", "樱桃", "苹果", "樱桃", "苹果", "樱桃", "苹果" };
	// 统计水果出现的次
	BSTree<string, int> countTree;
	for (auto str : strs)
	{
		auto ret = countTree.Find(str);
		if (ret == NULL)
		{
			countTree.Insert(str, 1);
		}
		else
		{
			ret->_value++;
		}
	}
	countTree.InOrder();
}

int main()
{
	TestBSTree();

	return 0;
}